package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.BotanyTreeInfoDao;
import cn.topevery.garden.entity.po.BotanyTreeInfo;
import cn.topevery.garden.entity.query.BotanyTreeInfoQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-08-26
 */
@Repository
public class BotanyTreeInfoDaoImpl extends BaseDaoImpl<BotanyTreeInfo> implements BotanyTreeInfoDao {
    @Override
    public <P extends DbCsp> List<BotanyTreeInfo> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<BotanyTreeInfo> listPage(BotanyTreeInfoQuery query) {
        String table = "t_botany_tree_info tbti";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        String columns = "tbti.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tbti.c_db_create_date desc");
        sqlQueryMate
                .where(true, "tbti.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getType() != null, "tbti.c_type", ScriptConditionEnum.Eq, query.getType())
                .where(StringUtils.isNotEmpty(query.getName()), "tbti.c_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotEmpty(query.getTypeName()), "tbti.c_section", ScriptConditionEnum.Like, query.getTypeName())
                .where(StringUtils.isNotEmpty(query.getArea()), "tbti.c_distribution_area", ScriptConditionEnum.Like, query.getArea())
                .where(StringUtils.isNotBlank(query.getSection()), "tbti.c_section", ScriptConditionEnum.Eq, query.getSection())
                .where(StringUtils.isNotBlank(query.getDistributionArea()), "tbti.c_distribution_area", ScriptConditionEnum.Like, query.getDistributionArea())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, BotanyTreeInfo.class);
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    @Override
    public boolean isExit(String fieldName, String value) {
        String table = "t_botany_tree_info ";
        String columns = fieldName;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, fieldName, ScriptConditionEnum.Eq, value)
        ;
        List<BotanyTreeInfo> list = selectList(sqlQueryMate, BotanyTreeInfo.class);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 描述 获取统计数量
     *
     * @param name     the name
     * @param extentId the extent id
     * @return the count
     * @author wenzhao.lin
     * @created 2021 -08-25 09:24:05
     */
    @Override
    public Map getCount(String name, Integer extentId, String typeId) {
        String table = "t_botany_tree_info tp";

        String columns = "count(1) value,'古树名木数量' as name";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_type", ScriptConditionEnum.Eq, 2) // (1-后备 2-古树名木 3-稀有)
                .where(StringUtils.isNotBlank(name), "tp.c_name", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_extent_id", ScriptConditionEnum.Eq, extentId)
                .where(StringUtils.isNotEmpty(typeId), "tp.c_section", ScriptConditionEnum.Eq, typeId)
        ;
        return getOneMap(sqlQueryMate);
    }

    /**
     * 描述 通过类型获取列表
     *
     * @param name     the name
     * @param extentId the extent id
     * @param type     the type
     * @return the person list by type
     * @author wenzhao.lin
     * @created 2021 -08-25 09:25:36
     */
    @Override
    public List<BotanyTreeInfo> getListByType(String name, Integer extentId, String type) {
        String table = "t_botany_tree_info tp";

        String columns = "tp.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_type", ScriptConditionEnum.Eq, 2)
                .where(StringUtils.isNotBlank(name), "tp.c_name", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_extent_id", ScriptConditionEnum.Eq, extentId)
                .where(type != null, "tp.c_section", ScriptConditionEnum.Eq, type)
        ;
        return selectList(sqlQueryMate, BotanyTreeInfo.class);
    }

    /**
     * 描述 获取类型列表
     *
     * @param name     the name
     * @param extentId the extent id
     * @return the person type list
     * @author wenzhao.lin
     * @created 2021 -08-27 09:15:52
     */
    @Override
    public List<BotanyTreeInfo> getTypeList(String name, Integer extentId) {
        String table = "t_botany_tree_info tp";

        String columns = "distinct tp.c_section c_type, tp.c_section c_type_name ";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_type", ScriptConditionEnum.Eq, 2)
                .where(StringUtils.isNotBlank(name), "tp.c_name", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_extent_id", ScriptConditionEnum.Eq, extentId)
        ;

        List<BotanyTreeInfo> list = selectList(sqlQueryMate, BotanyTreeInfo.class);
        for (BotanyTreeInfo botanyTreeInfo : list) {
            if (StringUtils.isNotEmpty(botanyTreeInfo.getType())) {
                Map map = getCount(name, extentId, botanyTreeInfo.getType());
                botanyTreeInfo.setTotalNumber(String.valueOf(map.get("value")));
            }
        }
        return list;
    }
}
